/*
* @Author: Josh
* @Date:   2019-01-31 21:29:31
* @Last Modified by:   Administrator
* @Last Modified time: 2019-05-02 19:56:10
*/
'use strict';

require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');

var navSide       = require('page/common/nav-side/index.js'),
    _mall         = require('util/mall.js'),
    _order        = require('service/order-service.js'),
    Pagination    = require('util/pagination/index.js'),
    templateIndex = require('./index.string');

//page 逻辑部分
var page = {
    data : {
        listParam : {
            pageNum  : 1,
            pageSize : 5
        }
    },
    init : function(){
        this.onLoad();
    },
    onLoad : function(){
        this.loadOrderList();
        //初始化左侧菜单
        navSide.init({
            name : 'order-list'
        });
    },
    //加载订单列表
    loadOrderList : function(){
        var orderListHtml = '',
            _this         = this,
            $listCon      = $('.order-list-con');

        $listCon.html('<div class="loading"></div>');
        _order.getOrderList(this.data.listParam, function(res){
            // 渲染html
            orderListHtml = _mall.renderHtml(templateIndex, res);
            $listCon.html(orderListHtml);
            
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,   
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,   
                pageNum         : res.pageNum,   
                pages           : res.pages
            });
        }, function(errMsg){
            $listCon.html('<p class="err-tip">加载订单失败,请刷新后重试！</p>')
        });
    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        //将工具封装成类并判断是否已经引用
        this.pagination ? '' : this.pagination = new Pagination();
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pegeNum){
                _this.data.listParam.pageNum = pegeNum;
                _this.loadOrderList();
            }
        }));
    }
};
    
$(function(){
    page.init(); 
});